package com.shujia.day17.tcp;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {

        /*
            1.需要读取一个文件 -> IO流 -> 字符流 -> BufferedReader
            2.创建和服务端的连接
            3.写出数据到服务端
            4.接收服务端发送的回馈信息
         */

        FileReader fileReader = new FileReader("data/tcp.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        // 2.创建和服务端的连接
        Socket socket = new Socket("localhost", 7777);
        OutputStream outputStream = socket.getOutputStream();

        String line = null;
//        import java.io.IOException;import java.io.InputStream;
//

        //  3.写出数据到服务端
        while ((line = bufferedReader.readLine()) != null){
            outputStream.write(line.getBytes());
            outputStream.write("\r\n".getBytes());
            outputStream.flush();
        }
        // 如果客户端对当前的流进行关闭操作，那么通道也就结束了 对应Server端中的输入流也就关闭
        socket.shutdownOutput();

//        4.接收服务端发送的回馈信息
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = inputStream.read(bytes);
        System.out.println(new String(bytes,0,read));

        // 关闭
        bufferedReader.close();
        socket.close();
    }
}
